﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using System.Text;

namespace ExpertMIS.UserControl
{
    public partial class ucExpertList : System.Web.UI.UserControl
    {
        private void RefreshExpertList(string DomainOID)
        {
            ExpertBroker expertBroker = new ExpertBroker();
            List<String> domains = new List<string>();
            domains.Add(DomainOID);
            dlColumns.DataSource = expertBroker.GetExpertsByDomain(domains);
            dlColumns.DataBind();
        }

        private string GetExpertDomainHtml(ExpertEntity expert)
        {
            ExpertBroker expertBroker = new ExpertBroker();
            StringBuilder sb = new StringBuilder();

            foreach (DomainEntity domain in expertBroker.GetExpertDomains(expert.OID))
            {
                if (sb.Length > 0) sb.Append(",");
                sb.Append(string.Format("<a href='DomainIntroduce.aspx?oid={0}' alt=''>{1}</a>", domain.OID, domain.DomainName, domain.Remark));
            }

            return sb.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            RefreshExpertList(Request["oid"]);
        }

        protected void dlColumns_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ExpertEntity expert = e.Item.DataItem as ExpertEntity;
                AnswerBroker answerBroker = new AnswerBroker();

                if (expert != null)
                {
                    Label lblName = e.Item.FindControl("lblName") as Label;
                    if (lblName != null)
                    {
                        lblName.Text = expert.Name;
                    }

                    Label lblExpertise = e.Item.FindControl("lblExpertise") as Label;
                    if (lblExpertise != null)
                    {
                        lblExpertise.Text = expert.Expertise;
                    }

                    Label lblPosition = e.Item.FindControl("lblPosition") as Label;
                    if (lblPosition != null)
                    {
                        lblPosition.Text = string.Format("{0}{1}", expert.AdminPosition, string.IsNullOrEmpty(expert.Skills) ? "" : "、" + expert.Skills);
                    }

                    Label lblPhone = e.Item.FindControl("lblPhone") as Label;
                    if (lblPhone != null)
                    {
                        lblPhone.Text = string.Format("{0}{1}", expert.CellPhone, string.IsNullOrEmpty(expert.Phone) ? "" : "、" + expert.Phone);
                    }

                    int replayCount = answerBroker.GetUserReplyCount(expert.UserID);
                    int appectCount = answerBroker.GetUserReplyCount(expert.UserID);

                    Label lblReplyCount = e.Item.FindControl("lblReplyCount") as Label;
                    if (lblReplyCount != null)
                    {
                        lblReplyCount.Text = string.Format("{0}", replayCount);
                    }

                    Label lblAcceptCount = e.Item.FindControl("lblAcceptCount") as Label;
                    if (lblAcceptCount != null)
                    {
                        lblAcceptCount.Text = string.Format("{0}", appectCount);
                    }

                    Label lblAcceptRate = e.Item.FindControl("lblAcceptRate") as Label;
                    if (lblAcceptRate != null)
                    {
                        lblAcceptRate.Text = string.Format("{0}%", replayCount != 0 ? (appectCount / replayCount) * 100 : 0);
                    }
                }
            }
        }
    }
}